class EXAMPLE4
-- A small app to how build menus and use mnemonics and shortcut keys (also 
-- known as Accelerator or Hot Key). The example also show how use an file
-- dialog and a detach box. Notice only some tool bar buttons works.
-- See simple_notepad tutorial to a more complex example.
	
inherit
	IUP_INTERFACE

create {ANY}
   make

feature {ANY}

	code: INTEGER
	
   make
      local
 			gui: IUP
			i: STRING
			main_menu, menu1, menu2: IUP_MENU
			bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9: IUP_BUTTON
			lb1, lb2, lb3: IUP_LABEL
			im: IUP_IMAGE
			sub1, sub2: IUP_SUBMENU
			open, about, exit, size1, size2, size3: IUP_MENU_ITEM
			spt: IUP_MENU_SEPARATOR
			v: IUP_VBOX
			h: IUP_HBOX
			db: IUP_DETACH_BOX
			restore: IUP_BUTTON
			ml: IUP_MULTILINE
			w: IUP_DIALOG

      do
			gui := iup_open
			gui.load_images -- Load the predefined images at IUP stock.

			-- Get the code of the combination Ctrl+c, that will be used 
			-- to close the program. Here we should use always uppercase 
			-- letter, even if the combination use lower.
			code := gui.x_key_ctrl('C')

			-- Create the image for the "underline" tool-bar button.
			-- We should pass the width and height of the image created
			-- with the predefined colors we want use (1 is black and 15 
			-- is white). See IUP_IMAGE class for more info.
			create im.image( 20, 20, {ARRAY[INTEGER]} << 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																	  	   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15,  1, 15,  1, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1,  1,  1,  1,  1,  1,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15,  1, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
																		   15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15 >>)
			im.set_widget_name("my_image")

			-- Create the menu items.
			create open.item("Open...")
			open.set_hide_mark(True)
			open.set_cb_action(agent open_file)

			create spt.separator
			
			create about.item("About...")
			about.set_hide_mark(True)
			about.set_cb_action(agent show_about)

			create exit.item("Close%TCtrl+C")
			exit.set_hide_mark(True)
			exit.set_cb_action(agent terminate)

			create size1.item("14")
			size1.set_cb_action(agent set_text_size)

			create size2.item("16")
			size2.set_cb_action(agent set_text_size)

			create size3.item("18")
			size3.set_cb_action(agent set_text_size)

			-- Create menus 1 and 2
			create menu1.menu({ARRAY[IUP_MENU_ELEMENT]} << open, spt, about, exit >>)
			create menu2.menu({ARRAY[IUP_MENU_ELEMENT]} << size1, size2, size3 >>)
			menu2.set_radio(True)

			-- Create submenus
			create sub1.submenu("&App", menu1)
			create sub2.submenu("Text Size", menu2)

			-- Create main menu and set its name
			create main_menu.menu({ARRAY[IUP_MENU_ELEMENT]} << sub1, sub2 >>)
			main_menu.set_widget_name("Menu")

			-- Tool bar
			create bt1.button("")
			bt1.set_image("IUP_FileNew")
			bt1.set_flat(True)
			bt1.set_can_focus(False)
			create bt2.button("")
			bt2.set_image("IUP_FileOpen")
			bt2.set_flat(True)
			bt2.set_can_focus(False)
			create bt3.button("")
			bt3.set_image("IUP_FileSave")
			bt3.set_flat(True)
			bt3.set_can_focus(False)
			create lb1.label_empty
			lb1.set_vertical_separator
			create bt4.button("")
			bt4.set_image("IUP_EditUndo")
			bt4.set_flat(True)
			bt4.set_can_focus(False)
			create bt5.button("")
			bt5.set_image("IUP_EditRedo")
			bt5.set_flat(True)
			bt5.set_can_focus(False)
			create lb2.label_empty
			lb2.set_vertical_separator
			create bt6.button("")
			bt6.set_image("IUP_EditCut")
			bt6.set_flat(True)
			bt6.set_can_focus(False)
			create bt7.button("")
			bt7.set_image("IUP_EditCopy")
			bt7.set_flat(True)
			bt7.set_can_focus(False)
			create bt8.button("")
			bt8.set_image("IUP_EditPaste")
			bt8.set_flat(True)
			bt8.set_can_focus(False)
			bt8.set_cb_action(agent paste)
			create lb3.label_empty
			lb3.set_vertical_separator
			create bt9.button("")
			bt9.set_flat(True)
			bt9.set_can_focus(False)
			bt9.set_image("my_image")
			bt9.set_cb_action(agent underline)
			create restore.button("Restore")
			restore.set_name("RESTORE")
			restore.set_cb_action(agent restore_bar)
			restore.set_visible(False)

			-- Create multiline
			create ml.multiline
			ml.set_name("TEXT")
			ml.expand_both_directions
			ml.set_formatting(True)
			ml.set_font_size(14)

			-- Put the buttons inside a horizontal box and inside the detachbox.
	      create h.hbox({ARRAY[IUP_WIDGET]} << bt1, bt2, bt3, lb1, bt4, bt5, lb2,
															 bt6, bt7, bt8, lb3, bt9, restore >>)
			
			-- Set a vertical margin (with some themes, the icons at toolbar 
			-- could overlap the line draw at the bottom of menu).
			h.set_margin(0, 8)

			create db.detach_box(h)
			db.set_name("DB")
			db.set_cb_detached(agent detach_bar)

			-- Put the detach box and multiline inside a vertical box.
	      create v.vbox({ARRAY[IUP_WIDGET]} << db, ml >>)

			-- Create the window.
	      create w.dialog(v)
			w.set_cb_k_any(agent key)
			w.set_title("Simple note pad")
			w.set_menu("Menu")
			w.set_predefined_size("HALF", "HALF")
	      i := w.show

         gui.main_loop
			gui.close
      end

	restore_bar(widget: IUP_BUTTON): STRING
		do
			widget.set_visible(False)

			if attached {IUP_DETACH_BOX} widget.get_dialog_child("DB") as dbox then
				dbox.restore_widget("NULL")
			end
			
			Result := "IUP_DEFAULT"
		end

	detach_bar(wgt: IUP_DETACH_BOX; dlg: IUP_DIALOG; x: INTEGER; y: INTEGER): STRING
		do
			if attached {IUP_BUTTON} wgt.get_dialog_child("RESTORE") as btn then
				btn.set_visible(True)
			end
			
			Result := "IUP_DEFAULT"
		end

	open_file (widget: IUP_MENU_ITEM): STRING
		local
			status: INTEGER
			rv, name: STRING
			fd: IUP_FILE_DIALOG
		do
			create fd.file_dialog
			fd.set_dialog_type("OPEN")
			fd.set_ext_filter("Text files|*.txt|")
			fd.set_title("Select a text file")

			rv := fd.popup_predefined_xy("IUP_CENTER", "IUP_CENTER")

			if rv.is_equal("IUP_NOERROR") then
				status := fd.get_status

				-- If user has selected a file, get its name and add this 
				-- (the name) to the multiline.
				if status.is_equal(0) then
					name := fd.get_value
				else
					name := ""
				end

				fd.destroy -- We should destroy the dialog.

				if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
					text.append(name)
				end
			end

			Result := "IUP_DEFAULT"
		end

	show_about (widget: IUP_MENU_ITEM): STRING
		local
			msg: IUP_MESSAGE
		do
			create msg.message("About", "Simple example with eiffel-iup")
			Result := "IUP_DEFAULT"
		end

	terminate (widget: IUP_MENU_ITEM): STRING
		do
			Result := "IUP_CLOSE"
		end

	set_text_size (widget: IUP_MENU_ITEM): STRING
		local
			title: STRING
		do
			title := widget.get_title

			if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
				if title.is_equal("14") then
					text.set_font_size(14)
				elseif title.is_equal("16") then
					text.set_font_size(16)
				else
					text.set_font_size(18)
				end
			end
			
			Result := "IUP_DEFAULT"
		end

	key (dlg: IUP_DIALOG; k: INTEGER): STRING
		do
			if code.is_equal(k) then
				Result := "IUP_CLOSE"
			else
				Result := "IUP_DEFAULT"
			end
		end

	underline (widget: IUP_BUTTON): STRING
		local
			underline_tag: IUP_FORMATTING
			tup: TUPLE[INTEGER, INTEGER]
		do
			-- Create the formatting tag for underline text.
			create underline_tag.formatting
			underline_tag.set_underline("SINGLE")

			if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then

				-- Get current selection (if any)
				tup := text.get_multiline_selection_pos

				--underline_tag.set_widget_name("UND")
				-- Set selection at tag.
				underline_tag.set_selection_pos(tup.integer_32_item(1), tup.integer_32_item(2))
			
				text.add_format_widget(underline_tag)
				--ml.add_format_tag("UND")
			end

			Result := "IUP_DEFAULT"
		end

	paste (widget: IUP_BUTTON): STRING
		local
			cp: IUP_CLIPBOARD
			s: STRING
		do
			cp := get_clipboard

			if cp.is_text_available then
				s := cp.get_text

				if attached {IUP_MULTILINE} widget.get_dialog_child("TEXT") as text then
					text.append(s)
				end
			end
			
			Result := "IUP_DEFAULT"
		end

end -- class EXAMPLE4
